package com.currency.mall.ui.guide;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.mifengs.commonbase.base.BaseActivity;
import com.currency.mall.R;
import com.currency.mall.ui.login.LoginActivity;
import com.currency.mall.util.CleanMessageUtil;

import java.util.ArrayList;

public class GuideActivity extends BaseActivity implements View.OnClickListener, ViewPager.OnPageChangeListener {

    // 定义ViewPager对象
    private ViewPager viewPager;

    // 定义ViewPager适配器
    private ViewPagerAdapter vpAdapter;

    // 定义一个ArrayList来存放View
    private ArrayList<View> views;

    private static final int PAGE_SIZE = 3;

    // 底部小点的图片
    private ImageView[] points;

    // 记录当前选中位置
    private int currentIndex;

    private LinearLayout mLlIndicator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //无title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
                WindowManager.LayoutParams. FLAG_FULLSCREEN);
        setContentView(R.layout.activity_guide);
        init();
    }

    private void init() {
        try {
            CleanMessageUtil.clearAllCache(getContext());
        }catch (Exception e){

        }
        mLlIndicator = (LinearLayout) findViewById(R.id.ll_indicator);

        // 实例化ArrayList对象
        views = new ArrayList<View>();

        // 实例化ViewPager
        viewPager = (ViewPager) findViewById(R.id.viewpager);

        // 实例化ViewPager适配器
        vpAdapter = new ViewPagerAdapter(views);

        // 定义一个布局并设置参数
        LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);

        // 初始化引导图片列表
        for (int i = 0; i < PAGE_SIZE; i++) {
            View v = null;
            switch (i) {
                case 0:
                    v = LayoutInflater.from(GuideActivity.this).inflate(R.layout.activity_guide_step, null);
                    break;
                case 1:
                    v = LayoutInflater.from(GuideActivity.this).inflate(R.layout.activity_guide_step02, null);
                    break;
                case 2:
                    v = LayoutInflater.from(GuideActivity.this).inflate(R.layout.activity_guide_step_last, null);
                    v.findViewById(R.id.bt_go).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(GuideActivity.this,LoginActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    });
                    break;
                default:
                    break;
            }
            views.add(v);
        }

        // 设置数据
        viewPager.setAdapter(vpAdapter);

        // 设置监听
        viewPager.setOnPageChangeListener(this);

        // 初始化底部小点
        initPoint();

    }

    /**
     * 初始化底部小点
     */
    private void initPoint() {

        points = new ImageView[PAGE_SIZE];

        // 循环取得小点图片
        for (int i = 0; i < PAGE_SIZE; i++) {
            // 得到一个LinearLayout下面的每一个子元素
            points[i] = (ImageView) mLlIndicator.getChildAt(i);
            // 默认都设为灰色
            points[i].setEnabled(true);
            // 给每个小点设置监听
            points[i].setOnClickListener(this);
            // 设置位置tag，方便取出与当前位置对应
            points[i].setTag(i);
        }

        // 设置当面默认的位置
        currentIndex = 0;
        // 设置为白色，即选中状态
        points[currentIndex].setEnabled(false);
    }


    private class ViewPagerAdapter extends PagerAdapter {

        // 界面列表
        private ArrayList<View> views;

        public ViewPagerAdapter(ArrayList<View> views) {
            this.views = views;
        }

        /**
         * 获得当前界面数
         */
        @Override
        public int getCount() {
            if (views != null) {
                return views.size();
            }
            return 0;
        }

        /**
         * 初始化position位置的界面
         */
        @Override
        public Object instantiateItem(View view, int position) {

            ((ViewPager) view).addView(views.get(position), 0);

            return views.get(position);
        }

        /**
         * 判断是否由对象生成界面
         */
        @Override
        public boolean isViewFromObject(View view, Object arg1) {
            return (view == arg1);
        }

        /**
         * 销毁position位置的界面
         */
        @Override
        public void destroyItem(View view, int position, Object arg2) {
            ((ViewPager) view).removeView(views.get(position));
        }
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        // 设置底部小点选中状态
        setCurDot(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {

    }

    /**
     * 设置当前的小点的位置
     */
	private void setCurDot(int position) {
		if (position < 0 || position > PAGE_SIZE - 1 || currentIndex == position) {
			return;
		}
		points[position].setEnabled(false);
		points[currentIndex].setEnabled(true);

		currentIndex = position;

	}


}
